package com.ghsc.ghxc.jsb.ui.live.shopping

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cn.appcore.base.BaseVMFragment
import com.cn.appcore.event.RxBus
import com.cn.appcore.event.RxBusBean
import com.cn.appcore.event.SchedulerTransformer
import com.cn.appcore.ext.*
import com.cn.appcore.utils.GlideUtil
import com.cn.appcore.utils.GsonUtil
import com.cn.appcore.widget.CustomRefreshListener
import com.cn.appcore.widget.RefreshHelper
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.CustSignKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.databinding.FragmentLiveCartListBinding
import com.ghsc.ghxc.jsb.ui.integral.activity.IntegralGoodsDetailActivity
import com.ghsc.ghxc.jsb.view.NotDataView
import com.scwang.smart.refresh.layout.api.RefreshLayout

/**
 * 项目：GuoHuaMall
 * 包名：com.ghsc.ghxc.ui.live.shopping.fragment
 * 类名：LiveAudienceFragment
 * 创建时间：2023/7/27 01:57
 * 创建人 @author： xiaolin
 * 描述 ：Todo...  直播间的 观众段 显示的小黄车列表
 */
class LiveAudienceFragment  : BaseVMFragment<LiveCarViewModel, FragmentLiveCartListBinding>(R.layout.fragment_live_cart_list) {

    //直播间id
    private var liveId :String =""
    //主播id
    private  var anchorId:String =""

    companion object{
        fun newInstance(anchorId :String,liveId :String): LiveAudienceFragment {
            val fragment = LiveAudienceFragment()
            fragment.liveId = liveId
            fragment.anchorId=anchorId
            return fragment
        }
    }

    override fun attachVMClass(): Class<LiveCarViewModel>? {
        return LiveCarViewModel::class.java
    }

    override fun lazyLoad() {

    }


    private val mAdapter: ListAdapter by lazy { ListAdapter() }
    var mList = arrayListOf<CommodityDataBean>()
    private var notDataView: View? = null

    var itemPosition=-1

    override fun initView(savedInstanceState: Bundle?) {
        binding.llTop.visibility=View.GONE
        binding.rvList.layoutManager = LinearLayoutManager(requireActivity())
        binding.rvList.adapter = mAdapter
        notDataView = NotDataView.notDataView(requireActivity(), binding.rvList, binding.refreshLayout,"暂无数据")
        mAdapter.setNewInstance(mList)
        mAdapter.setOnFastClickListener { data, position ->
//            startKtxActivity<CommodityDetailActivity>(
//                values = arrayListOf(
//                    "pid" to mAdapter.data[position].pid,
//                    "anchorId" to anchorId,
//                    "liveId" to liveId,)
//            )
            startKtxActivity<IntegralGoodsDetailActivity>(
                value =  "pid" to mAdapter.getItem(position).pid
            )
        }
        mAdapter.addChildClickViewIds(R.id.tvBtnSub)
        mAdapter.setOnFastChildClickListener { view, position ->
            startKtxActivity<IntegralGoodsDetailActivity>(
                value =  "pid" to mAdapter.getItem(position).pid
            )
        }
        initRefreshLayout()
        pageNo=1
        getListData()



        rxBusSubscription = RxBus.getDefault().toObservable(RxBusBean::class.java)
            .compose(SchedulerTransformer())
            .subscribe { rxBusBean ->
                when {
                    rxBusBean.id == CustSignKey.CUST_LIVE_GOODS_REFRESH && rxBusBean.sign == CustSignKey.CUST_LIVE_GOODS_REFRESH -> {
                        //上架。下架刷新
                        loge("获取上架。下架刷新 进行刷新")
                        pageNo=1
                        getListData()
                    }
                    rxBusBean.id == CustSignKey.CUST_LIVE_GOODS && rxBusBean.sign == CustSignKey.CUST_LIVE_GOODS -> {
                        //上架。下架刷新
                        loge("获取讲解中的数据 进行刷新")
                        pageNo=1
                        getListData()
                    }
                }
            }



    }

    private fun initRefreshLayout() {
        RefreshHelper.initRefreshLayout(binding.refreshLayout, object : CustomRefreshListener {
            override fun _onRefresh(refreshLayout: RefreshLayout?) {
                pageNo=1
                getListData()
            }

            override fun _onLoadMore(refreshLayout: RefreshLayout?) {
                pageNo++
                getListData()
            }
        })
    }

    private fun getListData() {
        val map: HashMap<String, Any> = HashMap()
        map["psize"] = 10
        map["pindex"] =pageNo
        map["uid"] = anchorId
        loge("商品列表参数：$map")
        GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onZbCartListMethod(it1) }

    }

    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@LiveAudienceFragment) {
                RefreshHelper.finishRefresh(binding.refreshLayout, pageNo)
                showToast(it)
            }
            againLoginMsg.observe(this@LiveAudienceFragment) {
                RefreshHelper.finishRefresh(binding.refreshLayout, pageNo)
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            }

            onZbCartListData.observe(this@LiveAudienceFragment) {
                RefreshHelper.finishRefresh(binding.refreshLayout, pageNo)
                if (pageNo == 1) {
                    if (it.data.list.isNullOrEmpty()){
                        mAdapter.replaceData( it.data.list)
                        mAdapter.setEmptyView(notDataView!!)
                    }else {
                        mAdapter.replaceData( it.data.list)
                    }
                } else {
                    if (!it.data.list.isNullOrEmpty())
                        mAdapter.addData(it.data.list)
                }
            }


        }
    }


    inner class ListAdapter : BaseQuickAdapter<CommodityDataBean, BaseViewHolder>(R.layout.live_cart_list_item) {
        override fun convert(holder: BaseViewHolder, item: CommodityDataBean) {

            GlideUtil.ImageLoad(this.context,item.images,holder.getView(R.id.img))
            holder.setText(R.id.tvTitle,item.title)
                .setText(R.id.tvNum,(holder.layoutPosition+1).toString())
                .setText(R.id.tvPrice,item.pay_jifen)
                .setGone(R.id.tvBtnExplain,true)
                .setGone(R.id.tvBtnAdd,true)
                .setGone(R.id.tvBtnDelete,true)
                .setGone(R.id.tvBtnSub,false)

            //是否讲解
            if(item.is_explain==1)
                holder.setGone(R.id.tvStatus,false)
            else holder.setGone(R.id.tvStatus,true)


        }
    }
}